import {defineStore} from 'pinia' //引入pinia
import router from '../../router/routerindex.js'

// import dbmysql from '../..//utils/mysqleasy.js'
// import dbutile from '../..//utils/utilssequelize.js'
// import Sequelize from 'sequelize';
// const Sequelize = require('sequelize');
// const mysql2 = require("mysql2");
// import * as mysql2  from "mysql2";

//这里官网是单独导出  是可以写成默认导出的  官方的解释为大家一起约定仓库用use打头的单词 固定统一小仓库的名字不易混乱
export const useStoreConnect = defineStore("Connect", {
  state: () => {
    return ({
      dialog_show: false,
      connecttree_data: [],
      connecttree_selectedKey: 1,
      connecttform: {
        sqltype: 'mysql',
        title: '本地连接',
        address: '127.0.0.1',
        port: '3306',
        username: 'root',
        password: 'root',
      },
      current_database: '',
      databaselist_tabledata: [],
      databaselist_tablerowSelectedKey: [],
      databaselist_tableloading: false,
      databaselist_tableDataChecked: [],

      datatablelist_tablecolumns: [],
      datatablelist_tabledata: [],
      datatablelist_tablerowSelectedKey: [],
      datatablelist_tableloading: false,
      datatablelist_tableDataChecked: [],
    })
  },
  actions: {
    async addConnect() {
      let obj = {
        sqltype: this.connecttform.sqltype,
        id: this.connecttform.sqltype + this.connecttform.address + this.connecttform.port + this.connecttform.username,
        title: this.connecttform.sqltype + '-' + this.connecttform.title,
        address: this.connecttform.address,
        port: this.connecttform.port,
        username: this.connecttform.username,
        password: this.connecttform.password,
      }

      this.connecttree_data.push(obj);

      this.dialog_show = false;

    },
    async initConnect(config) {
      console.log(config);
      $utilssequelize.init(config);

      const res = await sequelize.query("show databases", {raw: true});
      // console.log(res[0]);
      let children = [];
      res[0].forEach((item, index) => {
        // console.log(item);
        let tempdatabase = {
          sqltype: 'database',
          id: item.Database,
          title: item.Database,
        }

        children.push(tempdatabase)

      })

      this.connecttree_data.forEach((item, index) => {
        if (item.id == config.id) {
          this.connecttree_data[index]['children'] = children;
        }
      })

    },
    async selectDatabase(node) {
      this.current_database = node.title;
      this.databaselist_tableloading = true;
      // console.log(sequelize);
      const reschmao = await sequelize.query("SELECT * FROM information_schema.Tables WHERE TABLE_SCHEMA='" + node.title + "'", {raw: true});
      // console.log(reschmao[0]);
      this.databaselist_tabledata = reschmao[0];

      this.databaselist_tableloading = false;
      let children = [];
      let temptable = {
        sqltype: 'table',
        id: 'temptable',
        title: '表',
      }
      let tempviews = {
        sqltype: 'view',
        id: 'tempviews',
        title: '视图',
      }

      children.push(temptable);
      children.push(tempviews);
      console.log(node);
      this.connecttree_data.forEach((item, index) => {
        console.log(item);
        // if (item.id == config.id) {
        //   this.connecttree_data[index]['children'] = children;
        // }
      })
    },
    async queryTable(tablename) {
      this.datatablelist_tableloading = true;
      this.datatablelist_tablecolumns=[];
      const rescolumn = await sequelize.query(" SELECT * FROM `information_schema`.`COLUMNS` WHERE TABLE_SCHEMA='" + this.current_database + "' AND TABLE_NAME='" + tablename + "' ORDER BY ORDINAL_POSITION;");
      console.log(rescolumn[0]);
      rescolumn[0].forEach((item, index) => {
        let temp = {
          title:item.COLUMN_NAME+item.COLUMN_COMMENT,
          key:item.COLUMN_NAME,
          width:'150',
          ellipsisTooltip: true,
        }
        this.datatablelist_tablecolumns.push(temp);
      })

      // this.datatablelist_tablecolumns = rescolumn[0];
      // console.log(sequelize);
      const reschmao = await sequelize.query("SELECT * FROM `" + this.current_database + "`.`" + tablename + "` LIMIT 1000");
      console.log(reschmao[0]);
      this.datatablelist_tabledata = reschmao[0];

      this.datatablelist_tableloading = false;

    },
  },

})
